package com.stackexchange.webapis.methodgroups
{
	import com.stackexchange.webapis.client.StackExchangeService;
	import com.stackexchange.webapis.events.StackExchangeResultEvent;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	
	/**
	 * Broadcast as a result of the getById method being called
	 *
	 * The event contains the following properties
	 *	success	- Boolean indicating if the call was successful or not
	 *	data - When success is true, contains an "errors" StackExchangeError instance
	 *		   When success is false, contains an "error" StackExchangeError instance
	 *
	 * @see com.producteev.webapis.StackExchangeError
	 */
	[Event(name="errorsGetById", type="com.stackexchange.webapis.events.StackExchangeResultEvent")]
	
	public class Errors extends AbstractMethod
	{
		private static const GET_BY_ID:String = "{id}";
		
		public function Errors(service:StackExchangeService, methodCaller:MethodCaller, responseParser:ResponseParser)
		{
			super(service, methodCaller, responseParser);
		}
		
		/**
		 * Simulates an error given its code  
		 * 
		 * @param id id of the error to simulate
		 * 
		 * @see http://api.stackoverflow.com/1.0/help/method?method=errors/{id}
		 * 
		 **/
		public function getById(errorId:int):void
		{
			call(GET_BY_ID, getErrorByIdHandler, {id:errorId});
		}
		
		private function getErrorByIdHandler(event:Event):void
		{
			processAndDispatch(URLLoader(event.target).data,
				StackExchangeResultEvent.ERRORS_GET_BY_ID,
				_responseParser.parseError);
		}
	}
}